<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>欢迎使用领捷智慧物流管理系统</title>
    <link rel="hshortcut icon" href="../static/img/own/head.png"/>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i">
    <link rel="stylesheet" href="/static/fonts/fontawesome-all.min.css">
</head>

<body id="page-top">
    <div id="wrapper">
        <div class="d-flex flex-column" id="content-wrapper">
            <div id="content">
            <div class="container" style="min-width: 100%;width: 1162.4px;margin-bottom: 30px;padding-top: 3px;padding-right: 24px;padding-bottom: 12px;padding-left: 24px;">
                <div class="card row" style="margin: 0px;">
                    <div class="card-body border rounded" style="height: 79px;min-width: 0px;padding: 24px;padding-top: 23px;margin: 0px;margin-top: 0px;width: auto;">
                        <div class="row" style="width: auto;">

                            <div class="col" style="width: auto;">
                                <div class="dropdown text-xl-center" style="text-align: right;">
                                    <input style="height: 38px" type="text" placeholder="商品号" id="goodsid" name="goodsid">
                                </div>
                            </div>
                            <div class="col" style="width: auto;">
                                <div class="dropdown text-xl-center" style="text-align: right;">
                                    <input style="height: 38px" type="text" placeholder="商品名称" id="goodsname" name="goodsname">
                                </div>
                            </div>
                            <div class="col" style="width: auto;"align="right">
                                <div class="btn-group" role="group" style="padding-left: 1px;">
                                    <button class="btn btn-primary" type="button" id="searchGoodsByKeyWord">查询</button>
                                    <button class="btn btn-primary" type="button" style="margin-left: 5px;" id="clean">重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
            </div>
        </div>
        <div class="container-fluid">
            <div class="card shadow">
                <div class="card-header py-3" style="height: 75px;">
                    <div class="row" style="width: auto;height: auto;">
                        <div class="col">
                            <form id="importform" enctype="multipart/form-data" action="/goods/importGoods" method="post">
                                <div class="btn-group" role="group">
                                    <button class="btn btn-primary" type="button" style="width: 90px;" id="addProduct">新增</button>
                                    <button class="btn btn-primary" type="button" style="margin-left: 5px;width: 90px;" id="deleteProduct">删除</button>
                                    <button class="btn btn-primary" type="button" style="width: 90px;margin-left: 5px;" id="import" onclick="daoru()">导入</button>
                                    <input class="btn btn-primary" type="file" style="width: 90px;margin-left: 5px;" hidden id="file" name="file" onchange="getFile(value)">
                                    <button class="btn btn-primary" type="button" style="width: 90px;margin-left: 5px;" id="export">导出</button>
                                </div>
                            </form>

                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive table mt-2" id="dataTable-1" role="grid" aria-describedby="dataTable_info">
                        <table class="table my-0" id="dataTable">
                            <thead style="height: 71px;">
                                <tr style="height: 60px;text-align: center">
                                    <th style="width: 100px;height: 60px;"><input type="checkbox" style="margin-left: 1px;" id="selectAll">商品号</th>
                                    <th>商品名称</th>
                                    <th>单价</th>
                                    <th>备注</th>
                                    <th>库存</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="goods" style="text-align: center">
                            </tbody>
                            <tfoot>
                                <tr></tr>
                            </tfoot>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-md-6 align-self-center">
                            <p id="dataTable_info" class="dataTables_info" role="status" aria-live="polite">Showing 1 to 10 of 27</p>
                        </div>
                        <div class="col-md-6">
                            <nav class="d-lg-flex justify-content-lg-end dataTables_paginate paging_simple_numbers">
                                <ul class="pagination">
                                    <li class="page-item disabled"><a class="page-link" href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>
                                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                                    <li class="page-item"><a class="page-link" href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <footer class="bg-white sticky-footer">
        <div class="container my-auto">
            <div class="text-center my-auto copyright"><span>鄂ICP备2021022432号</span></div>
        </div>
    </footer>
    </div><a class="border rounded d-inline scroll-to-top" href="#page-top"><i class="fas fa-angle-up"></i></a></div>
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/bootstrap/js/bootstrap.min.js"></script>
    <script src="/static/js/chart.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.js"></script>
    <script src="/static/js/script.min.js"></script>
    <script src="/static/js/jquery.table2excel.min.js"></script>
    <script src="/static/js/jquery.session.js"></script>


    <script>
        // 初始化页面
        $(document).ready(function (){
            $.ajax({
                url: "/goods/allGoods",
                type: 'post',
                dataType:"json",
                success:function (result){
                    let str = "";
                    for(let i = 0; i < result.length; i++){
                        str +="<tr><td class=\"checkGoodsId\"><input type=\"checkbox\" style=\"margin-left: 1px;\" class=\"checkOne\">" + result[i].goodsid +"</td>" +
                            "<td>" + result[i].goodsname + "</td>" +
                            "<td>" + result[i].goodsprice + "</td>" +
                            "<td>" + result[i].goodsinfo + "</td>" +
                            "<td>" + result[i].goodsnum + "</td>" +
                            "<td><button class=\"btn btn-primary buhuo\" type=\"button\">补货</button></td></tr>";
                    }
                    $("#goods").append(str);
                    // alert("123456");
                }
            })
        })

        ////根据商品id，商品名称搜索商品
        $(function (){
            $("#searchGoodsByKeyWord").on("click",function (){
                var goodsname = $("#goodsname").val();
                var goodsid = $("#goodsid").val();
                $.ajax({
                    url: "/goods/searchGoodsByKeyWord",
                    type: 'post',
                    data: {"goodsname": goodsname,"goodsid": goodsid},
                    dataType:"json",
                    success:function (result){
                        $("#goods").html("");
                        let str = "";
                        for(let i = 0; i < result.length; i++){
                                str +="<tr><td class=\"checkGoodsId\"><input type=\"checkbox\" style=\"margin-left: 1px;\" class=\"checkOne\">" + result[i].goodsid +"</td>" +
                                    "<td>" + result[i].goodsname + "</td>" +
                                    "<td>" + result[i].goodsprice + "</td>" +
                                    "<td>" + result[i].goodsinfo + "</td>" +
                                    "<td>" + result[i].goodsnum + "</td>" +
                                    "<td><button class=\"btn btn-primary buhuo\" type=\"button\">补货</button></td></tr>";
                            }
                            $("#goods").append(str);
                    }
                })
            })
        })

        //清空搜索框
        $(function (){
            $("#clean").on("click",function (){
                $(location).attr("href","/page/toProduct")
            })
        })

        //跳转新增商品页面
        $(function (){
            $("#addProduct").on("click",function (){
                $(location).attr("href","/page/toProductAdd")
            })
        })


        //全选复选框
        $(function (){
            $("#selectAll").on("click",function (){
                var selectAll = document.getElementById("selectAll");
                // alert(selectAll.checked);
                if(selectAll.checked == true){
                    var checkOnes = document.getElementsByClassName("checkOne");
                    for(var i=0;i<checkOnes.length;i++){
                        checkOnes[i].checked=true;
                    }
                }else{
                    var checkOnes = document.getElementsByClassName("checkOne");
                    for(var i=0;i<checkOnes.length;i++){
                        checkOnes[i].checked=false;
                    }
                }
            })
        })

        //根据复选框删除订单
        $(function (){
            $("#deleteProduct").on("click",function (){
                var checkOnes = document.getElementsByClassName("checkOne");

                var obj = [];
                var k = 0;
                for(var i = 0;i < checkOnes.length;i++){
                    if(checkOnes[i].checked){
                        // var id = $(".checkOrderId").parent('tr').children().eq(0).text();
                        let id = $(".checkGoodsId").eq(i).text();
                        // alert(id);
                        obj[k++]=id;
                    }
                }
                // alert(obj);
                if(obj.length !== 0){

                    if(window.confirm("确认删除吗？")){
                        $.ajax({
                            url: "/goods/deleteGoods",
                            type: 'post',
                            dataType:"json",
                            data: {obj:obj},
                            traditional:true,
                            success:function (result){
                                alert("删除成功！");
                                $(location).attr("href","/page/toProduct")
                            }
                        })
                    }else{

                    }
                }else{
                    alert("请选择条目!");
                }


            })
        })

        $("#goods").on("click",".buhuo",function (){
            let buhuo = $(".buhuo").index(this);
            // alert(buhuo);
            let id = $(".checkGoodsId").eq(buhuo).text();
            // alert(id);
            $(location).attr("href","/goods/addInputorder?goodsid="+id);
        })

        //导出
        $(function (){
            $("#export").on("click",function (){
                $("#goods").table2excel({
                    exclude: ".buhuo",
                    name:"商品表",
                    filename:"商品表格"
                })
            })
        })

        //导入
        // $(function (){
        //     $("#import").on("click",function (){
        //             document.getElementById("file").click();
        //     })
        // })

        function daoru(){
            document.getElementById("file").click();
        }

        function getFile(value){
            var from = document.getElementById('importform');
            from.submit();
        }


        //session
        $.session.set("url",window.location.href);
    </script>
</body>

</html>